{extend name="public/base"}

{block name="title"}
收货地址
{/block}

{block name="style"}
{css href="/static/wechat/css/index.css" /}
{css href="/static/wechat/popo1/css/store.css" /}
{/block}

{block name="body"}
<div class="pageControl1 store add">
    <div class="nav">
        <div class="msui_navTop">
            <a class="msui_navTop-back" href="javascript:history.back();"><i class="micon-back_ff6"></i></a>
            <h1 class="msui_navTop-title">添加地址</h1>
        </div>
    </div>
    <div class="weui-top">
        <form action="?" method="post" id="address_form">
            <div class="weui-cells store_message">
                <div class="weui-cell">
                    <div class="">
                        <p class="fs08 weui-label">收货人</p>
                    </div>
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" placeholder="请输入联系人的姓名" name="linkman" value="{$data.linkman|default=''}"/>
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="">
                        <p class="fs08 weui-label">电话</p>
                    </div>
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" placeholder="请输入电话号码" name="phone" value="{$data.phone|default=''}"/>
                    </div>
                </div>
            </div>
            <div class="weui-cells">
                <a class="weui-cell weui-cell_access" href="javascript:;" style="padding: 15px;">
                    <div class="weui-cell__bd">
                        <input class="weui-input" id="start" name="s_shengshiqu" type="text" placeholder="省、市、区/县" value="" data-code="{$data.area|default=''}" data-codes="">
                    </div>
                    <div class="weui-cell__ft">

                    </div>
                    <input type="hidden" name="address_ids" value=""/>
                </a>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <select class="weui-select"  name="street" style="padding-left:0;">
                            <option>街道地址</option>
                        </select>
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" placeholder="详细地址" name="address" value="{$data.address|default=''}"/>
                    </div>
                </div>
                <div class="weui-cell">
                    <div class="">
                        <p class="fs08 weui-label">邮政编码</p>
                    </div>
                    <div class="weui-cell__bd">
                        <input type="text" class="weui-input" placeholder="请输入邮政编码" name="postcode" value="{$data.postcode|default=''}"/>
                    </div>
                </div>
            </div>
            <input type="hidden" name="id" value="{$data.id|default=0}">
            <a href="javascript:;" class="add_btn">保存</a>
        </form>
    </div>
</div>

<!-- loading toast -->
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast" style="opacity: 1; visibility: visible;">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>

{/block}


{block name="javascript"}
<script>
    $("#start").val("{$data.r_province|default=''} {$data.r_city|default=''} {$data.r_area|default=''}");
    if($("#start").val().length<5){
        $("#start").val('');
    }
    $("input[name='address_ids']").val("{$data.province|default=0},{$data.city|default=0},{$data.area|default=0}");

</script>
{js href="/static/wechat/jquery_weui/js/data-picker1.js" /}
{js href="/static/wechat/jquery_weui/js/city-picker.js" /}
<!--{js href="/static/wechat/popo1/js/shop.js" /}-->
<script>
    $(function () {
        $("input[name='s_shengshiqu']").cityPicker({
            title: "选择省市区",
            onChange: function (picker, values, displayValues) {
                return false;
            },
            onClose: function(r1){
                get_street()
            }
        });

        $(".add_btn").click(function () {
            $("input[name='address_ids']").val($("#start").data('codes'));
            var _obj = $("#address_form");
            $.ajax({
                type : 'post',
                url : _obj.attr('action'),
                data : _obj.serialize(),
                dataType : 'json',
                success : function (msg) {
                    if (msg.code == 200) {
                        alert('保存成功！！');
                        window.location.href = "{:url('w_address/index')}";
                    } else {
                        var code = msg.code;
                        $.alert(error_code[code]);
                    }
                    console.log(msg);
                }
            });
        });

        var _is_contact_address = true;
        $("#start").change(function () {
            _is_contact_address=true;
        });
        //get_street();
        $(document).on('click', ".picker-button:first", function () {
            get_street()
        });

        function get_street() {
            var street = '{$data.street|default=0}';
            if (_is_contact_address) {
                $('.weui-toast__content').html('获取数据...');
                $('#loadingToast').fadeIn();
                var area = $("#start").attr('data-code');
                $.get("{:url('wechat/base/get_street')}", {area: area}, function (msg) {
                    $('#loadingToast').fadeOut();
                    var html = [];
                    var data = msg.data;
                    var len = data.length;
                    html.push("<option value=''>请选择</option>");
                    for (var i = 0; i < len; i++) {
                        if (data[i].region_id == street)
                            html.push("<option value='"+ data[i].region_id +"' selected>"+ data[i].region_name +"</option>");
                        else
                            html.push("<option value='"+ data[i].region_id +"'>"+ data[i].region_name +"</option>");
                    }
                    $("select[name='street']").html(html.join(''));
                }, 'json');
            }
            _is_contact_address = false;
        }
    });

</script>
{/block}